package cn.com.thinvent.dispatch.web.controller;

import cn.com.thinvent.dispatch.entity.HouseholdAssets;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.entity.response.ReturnModel;
import cn.com.thinvent.dispatch.service.biz.HouseholdAssetsService;
import cn.com.thinvent.dispatch.service.biz.util.KeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/household-assets")
public class HouseholdAssetsController {

    @Autowired
    private HouseholdAssetsService householdAssetsService;

    @GetMapping
    public ReturnModel getAllHouseholdAssets(@RequestParam(defaultValue = "1") int page,
                                             @RequestParam(defaultValue = "10") int pageSize,
                                             @RequestParam(required = false) String assetName) {
        PagedGridResult<HouseholdAssets> pagedResult = householdAssetsService.getAllHouseholdAssets(page, pageSize, assetName);
        return new ReturnModel(200, pagedResult, "Success");
    }


    @GetMapping("/{id}")
    public ReturnModel getHouseholdAssetsById(@PathVariable String id) {
        HouseholdAssets householdAssets = householdAssetsService.getHouseholdAssetsById(id);
        return new ReturnModel(200, householdAssets, "Success");
    }

    @PostMapping
    public ReturnModel addHouseholdAssets(@RequestBody HouseholdAssets householdAssets) {
        householdAssets.setId(KeyGenerator.generateKey());
        Date date = new Date();
        householdAssets.setCreatedAt(date);
        householdAssets.setUpdatedAt(date);
        householdAssetsService.addHouseholdAssets(householdAssets);
        return new ReturnModel(201, null, "Created");
    }

    @PutMapping("/{id}")
    public ReturnModel updateHouseholdAssets(@PathVariable String id, @RequestBody HouseholdAssets householdAssets) {
        householdAssets.setId(id);
        householdAssetsService.updateHouseholdAssets(householdAssets);
        return new ReturnModel(200, null, "Updated");
    }

    @DeleteMapping("/{id}")
    public ReturnModel deleteHouseholdAssets(@PathVariable String id) {
        householdAssetsService.deleteHouseholdAssets(id);
        return new ReturnModel(200, null, "Deleted");
    }
}
